﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
using Gwca.DotNet;

namespace Gwca.DotNet.Demo
{
    public partial class Form1 : Form
    {
        System.Windows.Forms.Timer UpdateInfoTimer;

        public Form1()
        {
            InitializeComponent();
            Gw.Cgui = this.Handle;
            this.KeyPreview = true;

             UpdateInfoTimer = new System.Windows.Forms.Timer();

            UpdateInfoTimer.Interval = 2000;
            UpdateInfoTimer.Tick += new EventHandler(UpdateInfoTimer_Tick);
            UpdateInfoTimer.Start();

        }


        void UpdateInfoTimer_Tick(object sender, EventArgs e)
        {
            //** Check if GW is running.
            if (Gw.GetGwProcessHandle() != IntPtr.Zero)
            {
                //** Get player name and update the title bar.
                this.Text = "GWCA.NET Demo for " + Gw.GetName(-2);

                //** Get current zone id.
                zoneId.Text = Gw.CmdCb(Gw.Ca.GetMapId)[0].ToString();

                labelIsMoving.Text = "IsMoving: " + Gw.IsMoving(-2).ToString();
                labelIsDead.Text = "IsDead: " + Gw.IsDead(-2).ToString();
                labelIsAttacking.Text = "IsAttacking: " + Gw.IsAttacking(-2).ToString();

            }
        }

        protected override void WndProc(ref Message m)
        {
            if (m.HWnd == this.Handle)  //** Process messages sent to our application
            {
                switch (m.Msg)      
                {
                    case 0x500:
                        Gw.CbVar[0] = m.WParam.ToInt32();
                        Gw.CbVar[1] = m.LParam.ToInt32();
                     break;
                    default:
                     break;
                }
            }
            base.WndProc(ref m);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //** Check if GW is running.
            if(Gw.GetGwProcessHandle() == IntPtr.Zero)
            {
                MessageBox.Show("GWCA.NET could not locate a running instance of Guild Wars.\r\nApplication will not function properly until Guild Wars is running.", "GWCA.NET Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode.ToString() == "R")
            {
                Coordinates Sc = new Coordinates(Gw.GetCoords(-2));
                listBox1.Items.Add(Sc);

            }
        }

        public void RefreshInventoryInfo()
        {
            if (Gw.GetGwProcessHandle() != IntPtr.Zero)
            {
                treeView1.Nodes.Clear();

                for (int x = 1; x <= 5; x++)
                {
                    Bag newBag = new Bag(x);
                    TreeNode mainNode = new TreeNode();
                    mainNode.Name = "Bag" + x.ToString(); ;


                    mainNode.Text = System.Enum.GetName(typeof(Gw.Bag), newBag.BagIndex);

                    mainNode.Text += " (" + newBag.BagItems.ToString() + "/" + newBag.BagSize.ToString() + ")";
                    treeView1.Nodes.Add(mainNode);

                    for (int i = 1; i <= newBag.BagSize; i++)
                    {
                        if (newBag.Items[i].ItemId != 0)
                        {
                            TreeNode childNode = new TreeNode();

                            childNode.Name = "Item" + i.ToString();

                            childNode.Text = newBag.Items[i].ItemId.ToString() + " " + System.Enum.GetName(typeof(Gw.ItemRarity), newBag.Items[i].Rarity) + " " + (newBag.Items[i].Quanity > 1 ? "[" + newBag.Items[i].Quanity.ToString() + "]" : "");
                            treeView1.Nodes[newBag.BagIndex - 1].Nodes.Add(childNode);
                        }
                    }
                }
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Coordinates SelectedCoordinates = (Coordinates)listBox1.SelectedItem;

            if (SelectedCoordinates.x != 0 && SelectedCoordinates.y != 0)
            {
                if (radioButton1.Checked == true)
                {
                        Gw.MoveToEx(SelectedCoordinates.x, SelectedCoordinates.y, 50);
                }
                else if(radioButton2.Checked == true)
                {
                    Clipboard.SetText("MoveTo(" + SelectedCoordinates.x.ToString() + "," + SelectedCoordinates.y.ToString() + ")");
                }
                else if (radioButton3.Checked == true)
                {
                    Clipboard.SetText("Gw.MoveToEx(" + SelectedCoordinates.x.ToString() + ", " + SelectedCoordinates.y.ToString() + ");");
                }
            }     
        }

        private void BtnResign_Click(object sender, EventArgs e)
        {
            Gw.Resign();
        }

        private void BtnTravelTo_Click(object sender, EventArgs e)
        {
            Gw.TravelTo(Convert.ToInt32(zoneId.Text));
        }

        private void BtnRefreshInventory_Click(object sender, EventArgs e)
        {
            RefreshInventoryInfo();
        }

        private void BtnSendChat_Click(object sender, EventArgs e)
        {
            Gw.SendChat("Hello Team!", Gw.Chat.Team);
            Gw.SendChat("/clap");
        }
    }

    public class Coordinates
    {
        public float x;
        public float y;
        public DateTime dt;
        public int NearestAgentToCoordsId;
        public int NearestAgentToCoordsPtr;

        public Coordinates(float[] fCoords)
        {
            this.x = fCoords[0];
            this.y = fCoords[1];

            this.dt = DateTime.Now;
            this.NearestAgentToCoordsId = Gw.CmdCb(Gw.Ca.GetMyNearestAgent,0)[0];

           // this.NearestAgentToCoordsPtr = GWCA.CmdCb(GWCA.Ca.GetAgentPtr, this.NearestAgentToCoordsId)[0];
        }

        public override string ToString()
        {
            return string.Format("X: {0}; Y: {1}", this.x, this.y);
        }
    }

    public class Bag
    {
        public int BagIndex;
        public int BagSize;
        public int BagItems;

        public Item[] Items = new Item[21];

        public Bag(int bagIndex)
        {
            this.BagIndex = bagIndex;
            this.BagSize = Gw.CmdCb(Gw.Ca.GetBagSize, BagIndex)[0];

            InitItems();
        }

        private void InitItems()
        {
                for (int x = 1; x < BagSize + 1; x++)
                {
                    Items[x] = new Item(BagIndex, x);

                    if (Items[x].ItemId != 0)
                    {
                        BagItems++;
                        int[] ItemInfo = Gw.GetItemInfo(BagIndex, Items[x].Slot);
                        Items[x].Rarity = ItemInfo[0];
                        Items[x].Quanity = ItemInfo[1];
                    }

                }
        }
        
    }

    public class Item
    {
        public int ItemId;
        public int Slot;
        public int Rarity;
        public int Quanity;
        private int BagId;

        public Item(int bagId, int itemSlot)
        {
            this.BagId = bagId;
            this.Slot = itemSlot;
            ItemId = Gw.GetItemId(this.BagId, this.Slot);
        }


    }
}
